import BASE_URL from '@/api/env.js' //引入接口共用地址
import RequestManager from '@/utils/requestManager.js'
import {
	toast,
	clearStorageSync,
	getStorageSync,
	useRouter,
	getTimestamp
} from '@/utils/utils.js'
import signMd5Utils from '@/utils/signMd5Utils.js'
import systemConfig from '@/config/config.js';
const manager = new RequestManager()
const baseRequest = async (url, method, data = {}, formData, loading = true, ) => {
	// debugger
	let requestId = manager.generateId(method, url, data)
	if (!requestId) {
		console.log('重复请求')
	}
	if (!requestId) return false;
	let ContentType = formData ? 'application/x-www-form-urlencoded' : 'application/json'
	const header = {
		Authorization: getStorageSync('uni_id_token') || '',
		'X-Access-Token': getStorageSync('uni_id_token') || '',
		'X-Sign': signMd5Utils.getSign(url, data),
		'X-Timestamp': getTimestamp(),
		'X-Version': 'v3',
		'Content-type': ContentType,
	}
	return new Promise((reslove, reject) => {
		loading && uni.showLoading({
			title: 'loading'
		})
		uni.request({
			url: BASE_URL + url,
			method: method || 'GET',
			header: header,
			timeout: 10000,
			data: data || {},
			complete: () => {
				uni.hideLoading()
				manager.deleteById(requestId)
			},
			success: (successData) => {
				//console.log(successData)
				const res = successData.data
				if (res.code == 401) {
					clearStorageSync('uni_id_token')
					// useRouter(systemConfig.login_page, 'reLaunch')
					uni.reLaunch({
						url: '/pages/login/login'
					})
				} else {
					reslove(res)
				}
				if (successData.statusCode == 200) {
					// 业务逻辑，自行修改，401是服务器上返回该token过期，过期后跳转到登陆页面

				} else {
					console.log('网络连接失败，请稍后重试', url)
					toast('网络连接失败，请稍后重试')
					reject(res)
				}
			},
			fail: (msg) => {
				console.log("请求：" + BASE_URL + url + '，发生错误：', err)
				toast('网络连接失败，请稍后重试')
				reject(msg)
			}
		})
	})
}

const request = {};

['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
	request[method] = (api, data, loading) => baseRequest(api, method, data, loading)
})

export default request